﻿using System;
using System.IO;
using Carrotware.CMS.Core;

/*
* CarrotCake CMS
* http://www.carrotware.com/
*
* Copyright 2011, Samantha Copeland
* Dual licensed under the MIT or GPL Version 3 licenses.
*
* Date: October 2011
*/

namespace Carrotware.CMS.UI.Admin.c3_admin {

	public partial class PageImport : AdminBasePage {

		protected void Page_Load(object sender, EventArgs e) {
			Master.ActivateTab(AdminBaseMasterPage.SectionID.DataImport);

			lblWarning.Text = "";
			lblWarning.Attributes["style"] = "color: #000000;";

			CMSConfigHelper.CleanUpSerialData();
		}

		protected void btnUpload_Click(object sender, EventArgs e) {
			string sXML = "";
			if (upFile.HasFile) {
				using (StreamReader sr = new StreamReader(upFile.FileContent)) {
					sXML = sr.ReadToEnd();
				}
			}
			string sTest = "";
			if (!String.IsNullOrEmpty(sXML) && sXML.Length > 500) {
				sTest = sXML.Substring(0, 250).ToLowerInvariant();

				try {
					if (sTest.Contains("<contentpageexport xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">")
						|| sTest.Contains("<contentpageexport xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\">")) {
						ContentPageExport cph = ContentImportExportUtils.DeserializeContentPageExport(sXML);
						ContentImportExportUtils.AssignContentPageExportNewIDs(cph);
						ContentImportExportUtils.MapSiteCategoryTags(cph);
						ContentImportExportUtils.SaveSerializedDataExport<ContentPageExport>(cph.NewRootContentID, cph);

						if (cph.ThePage.ContentType == ContentPageType.PageType.ContentEntry) {
							Response.Redirect(SiteFilename.PageAddEditURL + "?importid=" + cph.NewRootContentID.ToString());
						} else {
							Response.Redirect(SiteFilename.BlogPostAddEditURL + "?importid=" + cph.NewRootContentID.ToString());
						}
					}

					if (sTest.Contains("<siteexport xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">")
						|| sTest.Contains("<siteexport xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\">")) {
						SiteExport site = ContentImportExportUtils.DeserializeSiteExport(sXML);
						ContentImportExportUtils.AssignSiteExportNewIDs(site);
						ContentImportExportUtils.SaveSerializedDataExport<SiteExport>(site.NewSiteID, site);

						Response.Redirect(SiteFilename.SiteImportURL + "?importid=" + site.NewSiteID.ToString());
					}

					if (sXML.Contains("<channel>") && sXML.Contains("<rss")) {
						int iChnl = sXML.IndexOf("<channel>");
						sTest = sXML.Substring(0, iChnl).ToLowerInvariant();
					}

					if (sTest.Contains("<!-- this is a wordpress extended rss file generated by wordpress as an export of your")
						&& sTest.Contains("http://purl.org/rss")
						&& sTest.Contains("http://wordpress.org/export")) {
						WordPressSite wps = ContentImportExportUtils.DeserializeWPExport(sXML);
						ContentImportExportUtils.AssignWPExportNewIDs(SiteData.CurrentSite, wps);
						ContentImportExportUtils.SaveSerializedDataExport<WordPressSite>(wps.NewSiteID, wps);

						Response.Redirect(SiteFilename.WPSiteImportURL + "?importid=" + wps.NewSiteID.ToString());
					}

					lblWarning.Text = "File did not appear to match an expected format.";
					lblWarning.Attributes["style"] = "color: #990000;";
				} catch (Exception ex) {
					lblWarning.Text = ex.ToString();
					lblWarning.Attributes["style"] = "color: #990000;";
				}
			} else {
				lblWarning.Text = "No file appeared in the upload queue.";
				lblWarning.Attributes["style"] = "color: #990000;";
			}
		}
	}
}